package com.jiutong.meipenggang.ui.activitys;

import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.jiutong.meipenggang.R;
import com.jiutong.meipenggang.base.AppManager;
import com.jiutong.meipenggang.base.BaseActivity;
import com.jiutong.meipenggang.common.Constants;
import com.jiutong.meipenggang.entity.BaseBean;
import com.jiutong.meipenggang.entity.LoginBean;
import com.jiutong.meipenggang.http.model.interfaces.RegisterAndLoginBiz;
import com.jiutong.meipenggang.http.present.UserInfoPresent;
import com.jiutong.meipenggang.utils.SpConfig;
import com.jiutong.meipenggang.utils.StringUtil;

import butterknife.Bind;
import butterknife.OnClick;

/**
 * @author Veken
 */
public class LoginActivity extends BaseActivity implements RegisterAndLoginBiz {


    @Bind(R.id.iv_login_cancel)
    ImageView ivLoginCancel;
    @Bind(R.id.iv_login_user)
    ImageView ivLoginUser;
    @Bind(R.id.et_user_name)
    EditText etUserName;
    @Bind(R.id.view_login1)
    View viewLogin1;
    @Bind(R.id.iv_login_pwd)
    ImageView ivLoginPwd;
    @Bind(R.id.et_user_pwd)
    EditText etUserPwd;
    @Bind(R.id.view_login2)
    View viewLogin2;
    @Bind(R.id.tv_forget_password_login)
    TextView tvForgetPasswordLogin;
    @Bind(R.id.btn_login)
    Button btnLogin;
    @Bind(R.id.tv_register)
    TextView tvRegister;

    private UserInfoPresent userInfoPresent;

    @Override
    public int getLayoutResouceId() {
        return R.layout.activity_login;
    }

    @Override
    protected void initView() {
        super.initView();
        //EditText监听事件
        etChangeListener();
        userInfoPresent = new UserInfoPresent(this, this);
    }

    //EditText监听事件
    private void etChangeListener() {
        etUserName.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (s != null && s.length() > 0) {
                    ivLoginUser.setBackgroundResource(R.mipmap.ic_phone_pre);
                    viewLogin1.setBackgroundResource(R.color.blue_btn);
                } else {
                    ivLoginUser.setBackgroundResource(R.mipmap.ic_phone);
                    viewLogin1.setBackgroundResource(R.color.line);
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });

        etUserPwd.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (s != null && s.length() > 0) {
                    ivLoginPwd.setBackgroundResource(R.mipmap.ic_lock_pre);
                    viewLogin2.setBackgroundResource(R.color.blue_btn);
                } else {
                    ivLoginPwd.setBackgroundResource(R.mipmap.ic_lock);
                    viewLogin2.setBackgroundResource(R.color.line);
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
    }


    @OnClick({R.id.iv_login_cancel, R.id.btn_login, R.id.tv_register, R.id.tv_forget_password_login})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_login_cancel:
                finish();
                break;
            case R.id.btn_login:
                if (isRightAction()) {
                    userInfoPresent.login();
                }
                break;
            case R.id.tv_register:
                openActivity(RegisterActivity.class);
                finish();
                break;
            case R.id.tv_forget_password_login:
                openActivity(PwdFrogetActivity.class);
                finish();
                break;
            default:
                break;
        }
    }

    private boolean isRightAction() {
        if (TextUtils.isEmpty(getPhoneNum())) {
            toastShortShow("请输入手机号");
            return false;
        }

        if (!StringUtil.isMobileNumber(getPhoneNum())) {
            toastShortShow("请输入正确的手机号");
            return false;
        }

        if (TextUtils.isEmpty(getOriPwd())) {
            toastShortShow("请输入密码");
            return false;
        }

        if (!StringUtil.isRightPwd(getOriPwd())) {
            toastShortShow("请输入正确的密码");
            return false;
        }
        return true;
    }

    @Override
    public String getPhoneNum() {
        return etUserName.getText().toString().trim();
    }

    @Override
    public String getVerificatCode() {
        return null;
    }

    @Override
    public String getOriPwd() {
        return etUserPwd.getText().toString().trim();
    }

    @Override
    public String getNewPwd() {
        return null;
    }

    @Override
    public void onVerCodeGetSuccess(BaseBean subjects) {

    }


    @Override
    public void onSuccess(Object object) {
        LoginBean respond =  (LoginBean) object;
        if (respond.getError()==-1) {
            toastShortShow(respond.getMsg());
            SpConfig.getInstance().edit().putString(Constants.TEPPHONE_STRING, getPhoneNum()).commit();
            SpConfig.getInstance().edit().putString(Constants.USERNAME_STRING, getPhoneNum()).commit();
            SpConfig.getInstance().edit().putString(Constants.ID,respond.getId()).commit();
            SpConfig.getInstance().edit().putString(Constants.TOKEN,respond.getToken()).commit();
            SpConfig.getInstance().edit().putBoolean(Constants.IS_LOGIN,true).commit();
            SpConfig.getInstance().edit().putString(Constants.GESTURE_CIPHER,respond.getGestureCipher()).commit();
            SpConfig.getInstance().edit().putInt(Constants.USER_ID,respond.getUserId()).commit();
            SpConfig.getInstance().edit().putBoolean(Constants.TOKEN_FAIL,false).commit();
            openActivity(GestureEditActivity.class);
            finish();
        } else {
                toastShortShow(respond.getMsg());
        }
    }

    @Override
    public void onError(Throwable e) {

    }

    @Override
    public int getOpt() {
        return 4;
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
//            openActivity(MainActivity.class);
            AppManager.getAppManager().finishActivity(this);
        }
        return super.onKeyDown(keyCode, event);
    }

}
